﻿using System;
using System.Collections.Generic;
using test8_h1.Models;
using test8_h1.Data;

namespace test8_h1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("程序启动中...");
                DatabaseHelper.InitializeDatabase();

                while (true)
                {
                    ShowMainMenu();
                    string choice = Console.ReadLine();
                    switch (choice)
                    {
                        case "1":
                            ManageSchools();
                            break;
                        case "2":
                            ManageClasses();
                            break;
                        case "3":
                            ManageStudents();
                            break;
                        case "4":
                            ViewLogs();
                            break;
                        case "0":
                            Console.WriteLine("感谢使用，再见！");
                            return;
                        default:
                            Console.WriteLine("无效的选择，请重试。");
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"启动时发生错误: {ex.Message}");
                Console.WriteLine("按任意键退出...");
                Console.ReadKey();
            }
        }

        static void ShowMainMenu()
        {
            Console.WriteLine("\n===========================================");
            Console.WriteLine("=== 学生管理系统 ===");
            Console.WriteLine("1. 学校管理");
            Console.WriteLine("2. 班级管理");
            Console.WriteLine("3. 学生管理");
            Console.WriteLine("4. 查看日志");
            Console.WriteLine("0. 退出");
            Console.Write("请选择操作：");
        }

        #region School Management
        static void ManageSchools()
        {
            while (true)
            {
                Console.WriteLine("\n===========================================");
                Console.WriteLine("=== 学校管理 ===");
                Console.WriteLine("1. 查看所有学校");
                Console.WriteLine("2. 添加学校");
                Console.WriteLine("3. 编辑学校");
                Console.WriteLine("4. 删除学校");
                Console.WriteLine("0. 返回主菜单");
                Console.Write("请选择操作：");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        DisplaySchools();
                        break;
                    case "2":
                        AddSchool();
                        break;
                    case "3":
                        EditSchool();
                        break;
                    case "4":
                        DeleteSchool();
                        break;
                    case "0":
                        return;
                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }
                
            }
        }

        static void DisplaySchools()
        {
            var schools = DataAccess.GetAllSchools();
            Console.WriteLine("\n当前所有学校：");

            foreach (var school in schools)
            {
                Console.WriteLine($"ID: {school.Id}");
                Console.WriteLine($"学校名称: {school.Name}");
                Console.WriteLine($"地址: {school.Address}");
                Console.WriteLine("------------------------");
            }
        }

        static void AddSchool()
        {
            Console.WriteLine("\n添加新学校");
            Console.Write("学校名称：");
            string name = Console.ReadLine();
            Console.Write("地址：");
            string address = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("学校名称不能为空！");
                return;
            }

            var school = new School { Name = name, Address = address };
            DataAccess.AddSchool(school);
            Console.WriteLine("学校添加成功！");
        }

        static void EditSchool()
        {
            DisplaySchools();
            Console.Write("\n请输入要编辑的学校ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("新的学校名称：");
                string name = Console.ReadLine();
                Console.Write("新的地址：");
                string address = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(name))
                {
                    Console.WriteLine("学校名称不能为空！");
                    return;
                }

                var school = new School { Id = id, Name = name, Address = address };
                DataAccess.UpdateSchool(school);
                Console.WriteLine("学校信息更新成功！");
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }

        static void DeleteSchool()
        {
            DisplaySchools();
            Console.Write("\n请输入要删除的学校ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("确定要删除这所学校吗？(Y/N)：");
                if (Console.ReadLine().ToUpper() == "Y")
                {
                    DataAccess.DeleteSchool(id);
                    Console.WriteLine("学校删除成功！");
                }
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }
        #endregion

        #region Class Management
        static void ManageClasses()
        {
            while (true)
            {
                Console.WriteLine("\n===========================================");
                Console.WriteLine("=== 班级管理 ===");
                Console.WriteLine("1. 查看所有班级");
                Console.WriteLine("2. 添加班级");
                Console.WriteLine("3. 编辑班级");
                Console.WriteLine("4. 删除班级");
                Console.WriteLine("0. 返回主菜单");
                Console.Write("请选择操作：");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        DisplayClasses();
                        break;
                    case "2":
                        AddClass();
                        break;
                    case "3":
                        EditClass();
                        break;
                    case "4":
                        DeleteClass();
                        break;
                    case "0":
                        return;
                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }
               
            }
        }

        static void DisplayClasses()
        {
            var classes = new List<Class>();
            foreach (var school in DataAccess.GetAllSchools())
            {
                classes.AddRange(DataAccess.GetClassesBySchool(school.Id));
            }
            Console.WriteLine("\n当前所有班级：");

            foreach (var cls in classes)
            {
                Console.WriteLine($"ID: {cls.Id}");
                Console.WriteLine($"班级名称: {cls.Name}");
                Console.WriteLine($"年级: {cls.Grade}");
                Console.WriteLine($"所属学校ID: {cls.SchoolId}");
                Console.WriteLine("------------------------");
            }
        }

        static void AddClass()
        {
            DisplaySchools();
            Console.WriteLine("\n添加新班级");
            Console.Write("班级名称：");
            string name = Console.ReadLine();
            Console.Write("年级：");
            if (!int.TryParse(Console.ReadLine(), out int grade))
            {
                Console.WriteLine("无效的年级！");
                return;
            }
            Console.Write("所属学校ID：");
            if (!int.TryParse(Console.ReadLine(), out int schoolId))
            {
                Console.WriteLine("无效的学校ID！");
                return;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("班级名称不能为空！");
                return;
            }

            var cls = new Class { Name = name, Grade = grade, SchoolId = schoolId };
            DataAccess.AddClass(cls);
            Console.WriteLine("班级添加成功！");
        }

        static void EditClass()
        {
            DisplayClasses();
            Console.Write("\n请输入要编辑的班级ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("新的班级名称：");
                string name = Console.ReadLine();
                Console.Write("新的年级：");
                if (!int.TryParse(Console.ReadLine(), out int grade))
                {
                    Console.WriteLine("无效的年级！");
                    return;
                }
                Console.Write("新的所属学校ID：");
                if (!int.TryParse(Console.ReadLine(), out int schoolId))
                {
                    Console.WriteLine("无效的学校ID！");
                    return;
                }

                if (string.IsNullOrWhiteSpace(name))
                {
                    Console.WriteLine("班级名称不能为空！");
                    return;
                }

                var cls = new Class { Id = id, Name = name, Grade = grade, SchoolId = schoolId };
                DataAccess.UpdateClass(cls);
                Console.WriteLine("班级信息更新成功！");
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }

        static void DeleteClass()
        {
            DisplayClasses();
            Console.Write("\n请输入要删除的班级ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("确定要删除这个班级吗？(Y/N)：");
                if (Console.ReadLine().ToUpper() == "Y")
                {
                    DataAccess.DeleteClass(id);
                    Console.WriteLine("班级删除成功！");
                }
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }
        #endregion

        #region Student Management
        static void ManageStudents()
        {
            while (true)
            {
                Console.WriteLine("\n===========================================");
                Console.WriteLine("=== 学生管理 ===");
                Console.WriteLine("1. 查看所有学生");
                Console.WriteLine("2. 添加学生");
                Console.WriteLine("3. 编辑学生");
                Console.WriteLine("4. 删除学生");
                Console.WriteLine("0. 返回主菜单");
                Console.Write("请选择操作：");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        DisplayStudents();
                        break;
                    case "2":
                        AddStudent();
                        break;
                    case "3":
                        EditStudent();
                        break;
                    case "4":
                        DeleteStudent();
                        break;
                    case "0":
                        return;
                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }
               
            }
        }

        static void DisplayStudents()
        {
            var students = DataAccess.GetAllStudents();
            Console.WriteLine("\n当前所有学生：");

            foreach (var student in students)
            {
                Console.WriteLine($"ID: {student.Id}");
                Console.WriteLine($"姓名: {student.Name}");
                Console.WriteLine($"年龄: {student.Age}");
                Console.WriteLine($"性别: {student.Gender}");
                Console.WriteLine($"所属班级ID: {student.ClassId}");
                Console.WriteLine("------------------------");
            }
        }

        static void AddStudent()
        {
            DisplayClasses();
            Console.WriteLine("\n添加新学生");
            Console.Write("姓名：");
            string name = Console.ReadLine();
            Console.Write("年龄：");
            if (!int.TryParse(Console.ReadLine(), out int age))
            {
                Console.WriteLine("无效的年龄！");
                return;
            }
            Console.Write("性别：");
            string gender = Console.ReadLine();
            Console.Write("所属班级ID：");
            if (!int.TryParse(Console.ReadLine(), out int classId))
            {
                Console.WriteLine("无效的班级ID！");
                return;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("学生姓名不能为空！");
                return;
            }

            var student = new Student { Name = name, Age = age, Gender = gender, ClassId = classId };
            DataAccess.AddStudent(student);
            Console.WriteLine("学生添加成功！");
        }

        static void EditStudent()
        {
            DisplayStudents();
            Console.Write("\n请输入要编辑的学生ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("新的姓名：");
                string name = Console.ReadLine();
                Console.Write("新的年龄：");
                if (!int.TryParse(Console.ReadLine(), out int age))
                {
                    Console.WriteLine("无效的年龄！");
                    return;
                }
                Console.Write("新的性别：");
                string gender = Console.ReadLine();
                Console.Write("新的所属班级ID：");
                if (!int.TryParse(Console.ReadLine(), out int classId))
                {
                    Console.WriteLine("无效的班级ID！");
                    return;
                }

                if (string.IsNullOrWhiteSpace(name))
                {
                    Console.WriteLine("学生姓名不能为空！");
                    return;
                }

                var student = new Student { Id = id, Name = name, Age = age, Gender = gender, ClassId = classId };
                DataAccess.UpdateStudent(student);
                Console.WriteLine("学生信息更新成功！");
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }

        static void DeleteStudent()
        {
            DisplayStudents();
            Console.Write("\n请输入要删除的学生ID：");
            if (int.TryParse(Console.ReadLine(), out int id))
            {
                Console.Write("确定要删除这个学生吗？(Y/N)：");
                if (Console.ReadLine().ToUpper() == "Y")
                {
                    DataAccess.DeleteStudent(id);
                    Console.WriteLine("学生删除成功！");
                }
            }
            else
            {
                Console.WriteLine("无效的ID！");
            }
        }
        #endregion

        static void ViewLogs()
        {
            Console.WriteLine("\n===========================================");
            Console.WriteLine("=== 操作日志 ===");
            var logs = DataAccess.GetAllLogs();
            Console.WriteLine("\n操作日志列表：");

            foreach (var log in logs)
            {
                Console.WriteLine($"ID: {log.Id}");
                Console.WriteLine($"操作类型: {log.Operation}");
                Console.WriteLine($"详细信息: {log.Details}");
                Console.WriteLine($"时间戳: {log.Timestamp}");
                Console.WriteLine("------------------------");
            }
            Console.WriteLine("\n按任意键返回主菜单...");
            Console.ReadKey();
        }
    }
}